package com.youtui.manager.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.NextIdUtil;
import com.youtui.manager.entity.ActualIncomeEntity;
import com.youtui.manager.mapper.ActualIncomeMapper;
import com.youtui.manager.model.Account;
import com.youtui.manager.model.ActualIncome;
import com.youtui.manager.model.ActualIncomeExample;
import com.youtui.manager.model.ActualIncomeExample.Criteria;
import com.youtui.manager.model.Developer;
import com.youtui.manager.service.IAccountService;
import com.youtui.manager.service.IActualIncomeService;
import com.youtui.manager.service.IDeveloperService;

@Service
public class ActualIncomeServiceImpl extends BaseServiceImpl implements IActualIncomeService {

	@Autowired
	ActualIncomeMapper actualIncomeMapper;

	@Autowired
	IDeveloperService developerService;

	@Autowired
	IAccountService accountService;

	@Override
	public PageInfo<ActualIncomeEntity> findActualIncomeEntityPageList(String appName, int page, int rows) {
		PageHelper.startPage(page, rows);

		List<ActualIncomeEntity> list = findActualIncomeEntityList(appName);
		PageInfo<ActualIncomeEntity> pageinfo = new PageInfo<>(list);
		return pageinfo;
		
	}

	@Override
	public List<ActualIncomeEntity> findActualIncomeEntityList(String appName) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(appName)) {
			map.put("appName", "%" + appName + "%");
		}
		List<ActualIncomeEntity> list = actualIncomeMapper.selectActualIncomeEntityList(map);
		return list;
	}

	@Override
	public int insertActualIncome(String[] s, Long excId) {
		// s[0] 日期 s[1] appname s[2] appkey s[3]uv s[4] 预计收益 s[5] 可提现收益

		ActualIncome income = new ActualIncome();
		income.setId(NextIdUtil.getNextId());
		income.setExcId(excId);
		income.setCurdate(DateUtils.convert2Date(s[0], DateUtils.DF_SHORT_STR));
		income.setAppName(s[1]);
		income.setAppKey(s[2]);
		income.setDealTime(new Date());
		income.setActual(new BigDecimal(s[3]));
		income.setStatus(1);

		int count = selectActualIncomeCount(income.getAppKey(), income.getCurdate());
		if (count > 0) {
			income.setStatus(0);
			income.setRemark("重复导入");
		}else{
			Developer dev = developerService.findDeveloperByAppkey(income.getAppKey());
			if (dev == null) {
				income.setStatus(0);
				income.setRemark("appkey查询开发者信息失败");
			}else{
				income.setDevId(dev.getId());
				Account account = accountService.findAccountByDevId(dev.getId());
				//更新账户信息
				if(account == null){
					income.setStatus(0);
					income.setRemark("账户信息错误");
				}else{		
					//更新账户信息
					accountService.updateAvailWallet(account.getId(), income.getActual());
				}
			}
		}
		actualIncomeMapper.insert(income);
		 
		// 返回成功插入条数
		if (income.getStatus() == 1) {

			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public int selectActualIncomeCount(String appKey, Date curdate) {
		ActualIncomeExample example = new ActualIncomeExample();
		Criteria criteria = example.createCriteria();
		criteria.andAppKeyEqualTo(appKey);
		criteria.andCurdateEqualTo(curdate);
		return actualIncomeMapper.countByExample(example);
	}

}
